package org.iipg.starter;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Properties;

public class Runjar {
	
	private ClassLoader loader;
	private String basePath;
	private String confFile;
	private String mainClassName;
	private String verClassName;
	private Properties startProps;
	
	public Runjar() {
		this.basePath = System.getProperty("starter.base");
		this.confFile = System.getProperty("starter.conf");
	}
	
	private boolean loadProps() {
		try {
			InputStream in = new BufferedInputStream(new FileInputStream(this.confFile));
			startProps = new Properties();
			startProps.load(in);
			startProps.put("BASEPATH", this.basePath);
			this.mainClassName = startProps.getProperty("MAINCLASS");
			this.verClassName = startProps.getProperty("VERCLASS");
			return true;
		} catch (Exception e) {
			System.out.println("Starter config file is missing or damaged! " + e.getMessage());
			return false;
		}
	}
	
	private String getMainClass() {
		return this.mainClassName;
	}

	private String getJavaVersion() {
		return System.getProperty("java.version");
	}

	private String getBasePath() {
		return this.basePath;
	}
	
	private void loadClasspath() {
		loader = ExtClassLoader.loadClasspath(startProps);
	}

	/**
	 * @param args
	 */
	public void run(String[] args) throws Throwable {
		// 利用反射取得class和method
		Class<?> mainClass = Class.forName(mainClassName, true, loader);
		Method main = mainClass.getMethod("main", new Class[] { Array
				.newInstance(String.class, 0).getClass() });
		try {
			// 运行方法
			main.invoke(null, new Object[] { args });
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	}
	
	private void version(String[] args) throws Throwable {
		// 利用反射取得class和method
		Class<?> mainClass = Class.forName(verClassName, true, loader);
		Method main = mainClass.getMethod("main", new Class[] { Array
				.newInstance(String.class, 0).getClass() });
		try {
			// 运行方法
			main.invoke(null, new Object[] { args });
		} catch (InvocationTargetException e) {
			throw e.getTargetException();
		}
	}

    public static void main(String[] args) {
    	String usage = "RunJar cmd args...";  
    	  
    	if (args.length < 1) {  
            System.err.println(usage);  
            System.exit(-1);  
        }  
        int firstArg = 0;  
        String cmd = args[firstArg++];  
        
        Runjar starter = new Runjar();
        
        if (!starter.loadProps()) {
        	System.exit(1);
        }
        
    	starter.loadClasspath();
    	String[] newArgs = Arrays.asList(args).subList(firstArg, args.length)
    			.toArray(new String[0]);

    	if ("start".equals(cmd)) {
        	System.out.println("Using Base Path    : " + starter.getBasePath());
        	System.out.println("Using Java Version : " + starter.getJavaVersion());
        	System.out.println("Using Main Class   : " + starter.getMainClass());
        	try {
        		starter.run(newArgs);
        	} catch (Throwable e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}
        } else if ("version".equals(cmd)) {
        	try {
        		starter.version(newArgs);
        	} catch (Throwable e) {
        		// TODO Auto-generated catch block
        		e.printStackTrace();
        	}

        }
    }
}
